<?php
	require_once(dirname(__FILE__).'/include/config.inc.php');
?>
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8">
		<title>地图</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="Cache-Control" content="no-transform">
		<meta http-equiv="Cache-Control" content="no-siteapp">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	</head>
	<body>
		<div>
            <select id="provinces">
                <option value="">请选择省份</option>
            </select>
            <select id="citys">
                <option value="">请选择市</option>
            </select>
            <select id="countys">
                <option value="">请选择县</option>
            </select>
        </div>

        <div>
        	<input type="text" name="street" id="street" />
        </div>

	    <script type="text/javascript" src="js/jquery.js"></script>
	    <script type="text/javascript">
            $(document).ready(function() {

                //  加载所有的省份
                $.ajax({
                    type: "get",
                    url: "th_city.php", // type=1表示查询省份
                    data: {"parent_id": "1", "type": "1"},
                    dataType: "json",
                    success: function(data) {
                        $("#provinces").html("<option value=''>请选择省份</option>");
                        $.each(data, function(i, item) {
                            $("#provinces").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
                        });
                    }
                });

                $("#provinces").change(function() {
                    $.ajax({
                        type: "get",
                        url: "th_city.php", // type =2表示查询市
                        data: {"parent_id": $(this).val(), "type": "2"},
                        dataType: "json",
                        success: function(data) {
                            $("#citys").html("<option value=''>请选择市</option>");
                            $.each(data, function(i, item) {
                                $("#citys").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
                            });
                        }
                    });
                });

                $("#citys").change(function() {
                    $.ajax({
                        type: "get",
                        url: "th_city.php", // type =2表示查询市
                        data: {"parent_id": $(this).val(), "type": "3"},
                        dataType: "json",
                        success: function(data) {
                            $("#countys").html("<option value=''>请选择县</option>");
                            $.each(data, function(i, item) {
                                $("#countys").append("<option value='" + item.region_id + "'>" + item.region_name + "</option>");
                            });
                        }
                    });
                });
            });

             $("#street").focus(function(){
             	if($("#citys").val() == ''){
             		alert('请选择省/市/区');
             	}else{
             		var addr = $("#provinces").find("option:selected").text() + $("#citys").find("option:selected").text() + $("#countys").find("option:selected").text();

             		$.ajax({
                        type: "get",
                        async:false,
                        url: "http://api.map.baidu.com/geocoder/v2/?address="+addr+"&output=json&ak=fKHIciQniDIz9QsdoIRAAjkF",
                        dataType: 'jsonp',
                        jsonp: "callback",
                        success: function(data) {
                            var lat = data.result.location.lat;
                            var lng = data.result.location.lng;
                            $.ajax({
		                        type: "get",
		                        async:false,
		                        url: "http://api.map.baidu.com/geocoder/v2/?ak=fKHIciQniDIz9QsdoIRAAjkF&location="+lat+','+lng+"&output=json&pois=1",
		                        dataType: 'jsonp',
		                        jsonp: "callback",
		                        success: function(nearby) {
		                        	 console.log(nearby.result.pois);
		                        }
		                    });
                            
                        }
                    });
             	}
             });
        </script>
	</body>
</html>
